package controller;

import java.io.IOException;
import java.util.List;
import javax.faces.context.FacesContext;
import model.dao.SprintDao;
import model.domain.Project;
import model.domain.Sprint;

public class sprintBean {

    public sprintBean() {
        this.selectedsprint = new Sprint();
        this.newsprint = new Sprint();
    }

    private Sprint selectedsprint;
    private Sprint newsprint;
    private Project project;

    public Sprint getSelectedsprint(){
        return this.selectedsprint;
    }

    public void setSelectedsprint(Sprint object){
        this.selectedsprint.setSprintID(object.getSprintID());
        this.selectedsprint.setProject(object.getProject());
        this.selectedsprint.setSprintNumber(object.getSprintNumber());
        this.selectedsprint.setObjective(object.getObjective());
        this.selectedsprint.setStart(object.getStart());
        this.selectedsprint.setEnd(object.getEnd());
        this.selectedsprint.setProductbacklogs(object.getProductbacklogs());
    }

    public Sprint getNewsprint(){
        return this.newsprint;
    }

    public void setNewsprint(Sprint object){
        this.newsprint = object;
    }

    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
        return this.project;
    }

    public List<Sprint> getByproject(){
        SprintDao home = new SprintDao();
        return home.findByProject(this.project);
    }

    public List<Sprint> getSprints(){
        SprintDao home = new SprintDao();
        return home.getAll();
    }

    public void submitCreate() throws IOException{

        SprintDao home = new SprintDao();

        Sprint auxSprint = new Sprint();
        auxSprint.setSprintNumber(this.newsprint.getSprintNumber());
        auxSprint.setObjective(this.newsprint.getObjective());
        auxSprint.setStart(this.newsprint.getStart());
        auxSprint.setEnd(this.newsprint.getEnd());
        auxSprint.setProject(this.project);

        home.insert(auxSprint);

        this.newsprint.setSprintID(null);
        this.newsprint.setSprintNumber(0);
        this.newsprint.setObjective("");
        this.newsprint.setStart(null);
        this.newsprint.setEnd(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("SprintList.xhtml");

    }

    public void submitEdit(){

        SprintDao home = new SprintDao();
        home.insertOrUpdate(this.selectedsprint);
    }

    public void submitDelete(){
        SprintDao home = new SprintDao();
        home.delete(this.selectedsprint);
    }

}